package com.library.admin.modules.role.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.library.admin.modules.role.entity.Role;
import com.library.admin.modules.role.vo.RoleVO;
import com.library.admin.modules.role.bo.RolePage;
import com.library.admin.modules.role.bo.RoleInsert;
import com.library.admin.modules.role.bo.RoleUpdate;
import com.library.admin.modules.role.mapper.RoleMapper;
import com.library.admin.modules.role.struct.RoleStructMapper;
import com.library.admin.modules.role.service.RoleService;
import com.library.admin.modules.rolemenu.service.RoleMenuService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jakarta.annotation.Resource;
import com.library.common.util.PageCovertUtil;

import java.util.Collections;
import java.util.List;


/**
 * @Author xyh
 * @Desc 角色信息表(Role)表服务实现类
 * @Date 2023-10-02 15:09:34
 */

@Service("roleService")
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Resource
    private RoleStructMapper roleStructMapper;
    @Resource
    private RoleMapper roleMapper;
    @Resource
    private RoleMenuService roleMenuService;


    @Override
    public IPage<RoleVO> queryByPage(RolePage page) {
        // 查询条件
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotEmpty(page.getTitle())) {
            queryWrapper.eq(Role::getTitle, page.getTitle());
        }
        // 查询分页数据
        Page<Role> rolePage = new Page<Role>(page.getCurrent(), page.getSize());
        IPage<Role> pageData = baseMapper.selectPage(rolePage, queryWrapper);

        //转换成vo
        IPage<RoleVO> records = PageCovertUtil.pageVoCovert(pageData, RoleVO.class);
        if (CollUtil.isNotEmpty(records.getRecords())) {
            records.getRecords().forEach(r -> {
                r.setMenuIds(roleMenuService.getMenuIdsByRoleIds(Collections.singleton(r.getId())));
            });
        }
        return records;
    }


    @Override
    public RoleVO queryById(Integer id) {
        Role role = baseMapper.selectById(id);
        return roleStructMapper.roleToRoleVO(role);
    }

    @Override
    public boolean insert(RoleInsert roleInsert) {
        Role role = roleStructMapper.insertToRole(roleInsert);
        save(role);
        return true;
    }


    @Override
    public boolean update(RoleUpdate roleUpdate) {
        Role role = roleStructMapper.updateToRole(roleUpdate);
        updateById(role);
        return true;
    }

    @Override
    public boolean deleteById(Integer id) {
        return removeById(id);
    }

    @Override
    public List<Role> getRoleListByRoles(List<Integer> roleIds) {
        List<Role> roleListByRoles = getRoleListByRoles(roleIds);
        return roleListByRoles;
    }

}
